package com.xiaoming.freshmenapp.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.xiaoming.freshmenapp.R;
import com.xiaoming.freshmenapp.bean.NewsBean;

import org.json.JSONObject;

import java.util.List;

public class NewsListActivity extends AppCompatActivity {

    private ImageView home_back;
    private TextView home_title;
    private ListView lv_news;
    private static final String TAG = "NewsListActivity";
    private List<NewsBean.ListBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_list);
        initView();
        initEvent();
        initData();

    }

    private void initData() {
        getData();

    }

    private void getData() {
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://10.2.64.83:8080/app5/news/list";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG, "onResponse: " + jsonObject.toString());
                Gson gson = new Gson();
                NewsBean newsBean = gson.fromJson(jsonObject.toString(), NewsBean.class);
                list = newsBean.getList();
                lv_news.setAdapter(new MyNewsAdapter());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(NewsListActivity.this, "网络错误" + volleyError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        queue.add(request);

    }

    private void initEvent() {


    }

    private void initView() {
        home_back = (ImageView) findViewById(R.id.home_back);
        home_title = (TextView) findViewById(R.id.home_title);
        home_title.setText("校园新闻");
        lv_news = (ListView) findViewById(R.id.lv_news);
    }

    private class MyNewsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (viewHolder == null){
                convertView = View.inflate(getApplicationContext(), R.layout.item, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            NewsBean.ListBean listBean = list.get(position);
                viewHolder.name.setText(listBean.getTitle());
                viewHolder.content.setText(listBean.getPostDate());

            return convertView;
        }

        public class ViewHolder {
            public View rootView;
            public TextView name;
            public TextView content;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.name = (TextView) rootView.findViewById(R.id.name);
                this.content = (TextView) rootView.findViewById(R.id.content);
            }

        }
    }
}